/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.tzconverter.patterneditor.PatternEditorModel;
import org.tzconverter.tzdisplay.TimeZoneDisplayModel;

/**
 * Default implementation of the TimeZoneDisplayModel interface.
 * 
 * 
 * @since May 15, 2009
 */

public class SharedModel implements TimeZoneDisplayModel, PatternEditorModel
{

  private final PropertyChangeSupport changeSupport;
  
  private final SimpleDateFormat formatTester;
  private final AppPrefs prefs;
  
  private Date utc;
  private String datePattern;
  
  public SharedModel(AppPrefs prefs) 
  {
    this.prefs = prefs;
    
    this.changeSupport = new PropertyChangeSupport(this);
    this.formatTester = new SimpleDateFormat();

    //Use the default pattern from the preferences.
    this.datePattern = "";
    this.utc = new Date();
  }
  
  /* (non-Javadoc)
   * @see org.tzconverter.tzdisplay.TimeZoneDisplayModel#getDate()
   */
  @Override
  public Date getDate()
  {
    return utc;
  }

  /* (non-Javadoc)
   * @see org.tzconverter.tzdisplay.TimeZoneDisplayModel#setDate(java.util.Date)
   */
  @Override
  public void setDate(Date utc)
  {
    Date old = this.utc;
    this.utc = utc;
    changeSupport.firePropertyChange(PROP_DATE, old, utc);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.tzdisplay.TimeZoneDisplayModel#setDatePattern(java.lang.String)
   */
  @Override
  public void setPattern(String datePattern)
  {
    String old = this.datePattern;
    this.datePattern = datePattern;
    changeSupport.firePropertyChange(PROP_PATTERN, old, datePattern);
  }
  
  @Override
  public String loadPattern()
  {
    setPattern(prefs.getPattern());
    return getPattern();
  }
  
  @Override
  public String loadDefaultPattern()
  {
    setPattern(AppPrefs.DEFAULT_PATTERN);
    return getPattern();
  }

  /* (non-Javadoc)
   * @see org.tzconverter.tzdisplay.TimeZoneDisplayModel#getDatePattern()
   */
  @Override
  public String getPattern()
  {
    return datePattern;
  }
  
  
  @Override
  public void savePattern()
  {
    prefs.setPattern(datePattern);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorModel#isValidPattern(java.lang.String)
   */
  @Override
  public boolean isValidPattern(String pattern)
  {
    try {
      formatTester.applyPattern(pattern);
      return true;
    } catch(IllegalArgumentException e)
    {
      return false;      
    }
  }

  /* (non-Javadoc)
   * @see org.tzconverter.tzdisplay.TimeZoneDisplayModel#addPropertyChangeListener(java.beans.PropertyChangeListener)
   */
  @Override
  public void addPropertyChangeListener(PropertyChangeListener listener)
  {
    changeSupport.addPropertyChangeListener(listener);  
  }

  /* (non-Javadoc)
   * @see org.tzconverter.tzdisplay.TimeZoneDisplayModel#removePropertyChangeListener(java.beans.PropertyChangeListener)
   */
  @Override
  public void removePropertyChangeListener(PropertyChangeListener listener)
  {
    changeSupport.removePropertyChangeListener(listener);
  }
}
